package com.whfc.emp.manager.impl;

import com.whfc.base.dto.AppSyncDTO;
import com.whfc.base.service.SyncConfigService;
import com.whfc.common.enums.SyncFlag;
import com.whfc.common.enums.SyncModule;
import com.whfc.common.enums.SyncPlatform;
import com.whfc.common.enums.SyncType;
import com.whfc.emp.dao.AppSyncRecordMapper;
import com.whfc.emp.dto.AppSyncRecordDTO;
import com.whfc.emp.entity.AppSync;
import com.whfc.emp.entity.AppSyncRecord;
import com.whfc.emp.manager.AppSyncManager;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClasssName AppSyncManagerImpl
 * @Description 数据同步信息表
 * @Author hw
 * @Date 2021/1/5 9:16
 * @Version 1.0
 */
@Service
public class AppSyncManagerImpl implements AppSyncManager {

    @Autowired
    private AppSyncRecordMapper appSyncRecordMapper;

    @DubboReference(interfaceClass = SyncConfigService.class, version = "1.0.0")
    private SyncConfigService syncConfigService;

    @Override
    public AppSync getByProjectIdAndPlatform(Integer projectId, SyncPlatform platform) {
        AppSyncDTO syncConfig = syncConfigService.getSyncConfig(platform, projectId);
        AppSync data = null;
        if (syncConfig != null) {
            data = new AppSync();
            data.setAppKey(syncConfig.getAppKey());
            data.setAppSecret(syncConfig.getAppSecret());
            data.setHost(syncConfig.getHost());
            data.setId(syncConfig.getId());
        }
        return data;
    }

    @Override
    public void addSyncData(SyncPlatform syncPlatform, SyncModule syncModule, Integer objectId) {
        AppSyncRecord appSyncRecord = new AppSyncRecord();
        appSyncRecord.setPlatform(syncPlatform.getValue());
        appSyncRecord.setObjectId(objectId);
        appSyncRecord.setSyncFlag(SyncFlag.UNSYNC.getValue());
        appSyncRecord.setSyncType(SyncType.ADD.getValue());
        appSyncRecord.setType(syncModule.getValue());
        appSyncRecordMapper.insertSelective(appSyncRecord);
    }

    @Override
    public void updateSyncData(SyncPlatform syncPlatform, SyncModule syncModule, Integer objectId) {
        Integer platform = syncPlatform.getValue();
        Integer type = syncModule.getValue();
        appSyncRecordMapper.updateSyncType(platform, type, objectId);
    }

    @Override
    public List<AppSyncRecordDTO> getSyncList(List<Integer> objectIds, Integer type, Integer platform) {
        return appSyncRecordMapper.selectSyncRecordList(objectIds, type, platform);
    }
}
